<?php

class OrderLineItemsController extends AppController {

    var $name = 'OrderLineItems';

    function index() {
        $this->OrderLineItem->recursive = 0;
        $this->set('orderLineItems', $this->paginate());
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid order line item', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('orderLineItem', $this->OrderLineItem->read(null, $id));
    }

    function add() {
        if (!empty($this->data)) {
            $this->OrderLineItem->create();
            if ($this->OrderLineItem->save($this->data)) {
                $this->Session->setFlash(__('The order line item has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The order line item could not be saved. Please, try again.', true));
            }
        }
        $orders = $this->OrderLineItem->Order->find('list');
        $users = $this->OrderLineItem->User->find('list');
        $menuItems = $this->OrderLineItem->MenuItem->find('list');
        $this->set(compact('orders', 'users', 'menuItems'));
    }

    /**
     * Adds the order line items in the order_line_items table.
     * 
     * @param <type> $orderArr
     */
    function addNewOrder($orderArr) {
        
        if (!empty($orderArr)) {
            $orderId = $orderArr['orderId'];
            $userId = $orderArr['userId'];
            $orderLineItems = $orderArr['order_line_items'];

            foreach($orderLineItems as $menu_item_id => $quantity) {

                $orderLineItemArr["user_id"] = $userId;
                $orderLineItemArr["order_id"] = $orderId;
                $orderLineItemArr["quantity"] = $quantity;
                $orderLineItemArr["menu_item_id"] = $menu_item_id;


                $this->OrderLineItem->create();
                if ($this->OrderLineItem->save($orderLineItemArr)) {
                    //$this->Session->setFlash(__('The order line item has been saved', true));
                    //$this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash(__('The order line item could not be saved. Please, try again.', true));
                }
                
            }
        }
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid order line item', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->OrderLineItem->save($this->data)) {
                $this->Session->setFlash(__('The order line item has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The order line item could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->OrderLineItem->read(null, $id);
        }
        $orders = $this->OrderLineItem->Order->find('list');
        $users = $this->OrderLineItem->User->find('list');
        $menuItems = $this->OrderLineItem->MenuItem->find('list');
        $this->set(compact('orders', 'users', 'menuItems'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for order line item', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->OrderLineItem->delete($id)) {
            $this->Session->setFlash(__('Order line item deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Order line item was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

}

?>